﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ChildrenLoadedHandler = TreeBuilder.Contracts.ChildrenLoadedHandler;
using AbstraX.ClientInterfaces;
using TreeBuilder.Contracts;
using ServerEntities = SolutionFramework.Entities;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace SolutionFramework.Entities.TreeBuilder.Client
{
    public class TreeNodeOperationNode : ITreeNodeOperationNode
    {
        public event ChildrenLoadedHandler ChildrenLoaded;
        private ServerEntities.TreeNodeOperationNode treeNodeOperationNode;
        private ITreeBuilderService service;
        private ObservableCollection<ITreeNodeBase> children;
        private IOperation operation;
        public ITreeNodeBase ReferenceNode { get; set; }
        public ITreeNodeBase ShortcutNode { get; set; }
        public event NodeOperationSuccessful RemovedSuccessfully;
        public event NodeOperationSuccessful UpdatedSuccessfully;

        public TreeNodeOperationNode(ITreeBuilderService service, ServerEntities.TreeNodeOperationNode treeNodeOperationNode)
        {
            this.treeNodeOperationNode = treeNodeOperationNode;
            this.service = service;
            this.operation = service.GenerateItemFromID<IOperation>(treeNodeOperationNode.Operation);
        }

        public Entity ServerEntity
        {
            get
            {
                return treeNodeOperationNode;
            }
        }

        public IOperation Operation
        {
            get
            {
                return operation;
            }
            set
            {
                operation = value;
            }
        }

        public IBase BaseObject
        {
            get 
            {
                return operation;
            }

            set
            {
                operation = (IOperation) value;
            }
        }

        public string ID
        {
            get
            {
                return treeNodeOperationNode.TreeNodeBaseID.ToString();
            }
        }

        public string Text
        {
            get
            {
                return treeNodeOperationNode.TreeNodeBase.Name;
            }

            set
            {
                treeNodeOperationNode.TreeNodeBase.Name = value;
                service.UpdateNode<ITreeNodeOperationNode>(this);
            }
        }

        public void LoadChildren()
        {
            children = service.LoadChildren(this);

            ((INotifyCollectionChanged)children).CollectionChanged += new NotifyCollectionChangedEventHandler(ChildrenCollectionChanged);
        }

        private void ChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ChildrenLoaded(this, EventArgs.Empty);
        }

        public void NoChildren()
        {
            ChildrenLoaded(this, EventArgs.Empty);
        }

        public System.Collections.Generic.IEnumerable<ITreeNodeBase> Children
        {
            get
            {
                return children;
            }
        }

        public string ParentID
        {
            get
            {
                return treeNodeOperationNode.TreeNodeBase.ParentNodeID.ToString();
            }
        }

        public void Remove()
        {
            var submitOperation = service.DeleteNode<ITreeNodeOperationNode>(this);

            submitOperation.Completed += (sender, e) =>
            {
                RemovedSuccessfully(this, e);
            };
        }

        public ITreeNodeBase ShallowClone()
        {
            throw new NotImplementedException();
        }

        public ITreeNodeBase DeepClone()
        {
            throw new NotImplementedException();
        }

        ITreeNodeServerEntity MvvmTreeView.IClonable<ITreeNodeServerEntity>.ShallowClone()
        {
            throw new NotImplementedException();
        }

        ITreeNodeServerEntity MvvmTreeView.IClonable<ITreeNodeServerEntity>.DeepClone()
        {
            throw new NotImplementedException();
        }

        public ITreeNodeBase InternalNodeObject
        {
            get { throw new NotImplementedException(); }
        }

        public float ChildOrdinal
        {
            get
            {
                return treeNodeOperationNode.TreeNodeBase.ChildOrdinal ?? 0;
            }
            set
            {
                treeNodeOperationNode.TreeNodeBase.ChildOrdinal = value;
                service.UpdateNode<TreeNodeOperationNode>(this);
            }
        }

        public bool HasLayoverImage<T>(out T image)
        {
            image = default(T);
            return false;
        }
    }
}
